package com.vishop.daos;

import java.util.Collection;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.vishop.models.Product;

@Repository
@Transactional
public class ProductDaoImpl
{
	@Autowired
	private SessionFactory sessionFactory;

	public Product getById(int id){
		return (Product) sessionFactory.getCurrentSession().get(Product.class, id);
	}

	@SuppressWarnings("unchecked")
	public Collection<Product> getAllProduct(){
		return sessionFactory.getCurrentSession().createQuery("from Product").list();
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Product> zoekProducten(String naam)
	{
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Product.class);
		criteria.add(Restrictions.ilike("naam", naam+"%"));
		return criteria.list();
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Product> getHerenProducten(){
		Query query = sessionFactory.getCurrentSession().createQuery("from Product where categorie.categorieId =:categorieId");
		query.setParameter("categorieId", 1);
		Collection productlijst = query.list();
		return productlijst;
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Product> getDamesProducten(){
		return sessionFactory.getCurrentSession().createQuery("from Product  where categorie_categorieId = 2").list();
	}
	
	public Collection<Product> getProductByMerk(int id){
		Query query = sessionFactory.getCurrentSession().createQuery("from Product where merk.merkId =:merkId");
		query.setParameter("merkId", id);
		Collection productlijst = query.list();
		return productlijst;
	}
	 
	 
	public int save(Product product)
	{
		return (Integer) sessionFactory.getCurrentSession().save(product);
	}
	
	public void update(Product product)
	{
		sessionFactory.getCurrentSession().merge(product);
	}
	
	public void delete(int id)
	{
		Product c = getById(id);
		sessionFactory.getCurrentSession().delete(c);
	}
	
}
